<?php
/**
 * 动作类
 * User: Administrator
 * Date: 2018/4/13
 * Time: 10:21
 */
require 'library/start.php';
auto_load_class('Action');

class Action
{
    /**
     *
     * @return array
     */
    public function test()
    {
        dump($_SERVER);
    }

    public function qr()
    {
        //生成二维码
        $content = input('content');
        $size    = input('size', 10);
        if (!$content) {
            return setJson(10, '内容不能为空');
        }
        $www      = 'asset/img/';
        $img_name = md5($content . $size) . '.png';
        $path     = SG_ROOT . $www;
        if (!is_dir($path)) {
            @mkdir($path, 0777, true);
        }
        $file    = $path . $img_name;
        $img_url = SITE_URL . '/' . $www . $img_name;
        if (is_file($file)) {
            return setJson(0, 'ok', $img_url);
        }
        $errorCorrectionLevel = 'L';
        \QRcode::png($content, $file, $errorCorrectionLevel, $size, 2);
        return setJson(0, 'ok', $img_url);
    }

    public function merge()
    {
        $max_file_count = 50;
        $position     = input('position');
        $src_url      = input('src');
        $tar_url      = input('tar');
        $scale_number = input('scale', 4);
        $gap          = input('gap', 5);
        $www          = 'asset/img/';
        $file_name    = uniqid() . '.png';
        $_path        = $www . $file_name;
        $file_path    = SG_ROOT . $_path;
        if (!is_dir(dirname($file_path))) {
            @mkdir(dirname($file_path), 0777, true);
        }
        $dir = dirname($file_path);
        $file_list = scandir($dir);
        if (count($file_list) > $max_file_count) {
            foreach ($file_list as $file_name) {
                if ($file_name != '.' && $file_name != '..') {
                    $tmp = $dir . '/' . $file_name;
                    if (is_file($tmp)) {
                        unlink($tmp);
                    }
                }
            }
        }
        $url_img = SITE_URL . '/' . $_path;
        if (!$src_url || !$tar_url || !filter_var($src_url, FILTER_VALIDATE_URL) || !filter_var($tar_url, FILTER_VALIDATE_URL)) {
            return setResult(1, '请正确原图或目标图url');
        }
        if (!file_get_contents($src_url)) {
            return setResult(2, '无效原url');
        }
        $bg             = imagecreatefromstring(file_get_contents($src_url));        //目标图象连接资源。
        $qr             = imagecreatefromstring(file_get_contents($tar_url));    //源图象连接资源。
        $bg_width       = imagesx($bg);           //背景图片宽度
        $bg_height      = imagesy($bg);          //背景图片高度
        $logo_width     = imagesx($qr);       //logo图片宽度
        $logo_height    = imagesy($qr);      //logo图片高度
        $logo_qr_width  = $bg_width / $scale_number;     //组合之后logo的宽度(占二维码的1/5)
        $scale          = $logo_width / $logo_qr_width;    //logo的宽度缩放比(本身宽度/组合后的宽度)
        $logo_qr_height = $logo_height / $scale;  //组合之后logo的高度
        switch ($position) {
            case 'right-top':
                $from_width  = $bg_width - $logo_qr_width - $gap;
                $from_height = $gap;
                break;
            case 'right-bottom':
                $from_width  = $bg_width - $logo_qr_width - $gap;
                $from_height = $bg_height - $logo_qr_height - $gap;
                break;
            case 'left-top':
                $from_width  = $gap;
                $from_height = $gap;
                break;
            case 'left-bottom':
                $from_width  = $gap;
                $from_height = $bg_height - $logo_qr_height - $gap;
                break;
            case 'center':
                $from_width  = ($bg_width - $logo_qr_width) / 2;
                $from_height = ($bg_height - $logo_qr_height) / 2;
                break;
            default:
                $from_width  = mt_rand(0, $bg_width) - $logo_qr_width;
                $from_height = mt_rand(0, $bg_height) - $logo_qr_height;
                break;
        }
        //重新组合图片并调整大小
        /*
         *  imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
         */
//        imagecopymerge($bg, $qr, 0, 0, 0, 0, $logo_qr_width, $logo_qr_height, 80);
        imagecopyresampled($bg, $qr, $from_width, $from_height, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        //输出图片
        imagepng($bg, $file_path);
        imagedestroy($bg);
        imagedestroy($qr);
        return setResult(0, 'success', $url_img);
    }
}